package com.puzzlebazar.client.puzzleinfo;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.puzzlebazar.client.main.control.PuzzleTokenFactory;
import com.puzzlebazar.client.main.view.GenericListView;
import com.puzzlebazar.client.main.view.UpdateTokenCallback;
import com.puzzlebazar.client.user.UserInfo;
import com.puzzlebazar.client.user.UserModel;
import com.puzzlebazar.client.util.Capabilities;
import com.puzzlebazar.client.util.ImageHyperlink;
import com.puzzlebazar.client.util.Observable;

public class HistoryListView  extends GenericListView implements UpdateTokenCallback {

	private static final String STYLE_PREFIX = "history";
	private static final int NB_COLUMNS = 2;

	final PuzzleTokenFactory tokenFactory;

	protected final HistoryList historyList;


	/**
	 * A composite widget responsible of displaying the list of all solutions
	 * and from which the user can play or continue a solution.
	 *
	 * @param historyList The associated history list model
	 * @param tokenFactory The factory to use to create the hyperlinks
	 */
	public HistoryListView(HistoryList historyList, PuzzleTokenFactory tokenFactory) {
		super( NB_COLUMNS );
		
		this.historyList = historyList;
		this.tokenFactory = tokenFactory;
				
		historyList.addObserver( this );	
	}


	@Override
	public void update(Observable observable, Object arg) {
		assert( observable == historyList );		
		updateView();		
	}

	@Override
	public void updateTokens() {
		updateView();
	}

	@Override
	public void release() {
		historyList.deleteObserver(this);
	}	

	@Override
	protected void refreshList() {
		historyList.refresh();
		historyList.notifyObservers();

	}
	

	@Override
	protected void addLineToTable(FlexTable table, int row, final int currentIndex) {		

		UserInfo userInfo = UserModel.get().getUserInfo();
		final PuzzleInfo puzzleInfo = historyList.getPuzzleInfo();
		final HistoryInfo historyInfo = historyList.getHistoryInfo(currentIndex);
		final String historyKey = historyInfo.getKey();
		HorizontalPanel buttons = new HorizontalPanel();
				
		// Add a new row to the table, then set each of its columns to the
		// puzzle's information
		if( Capabilities.canContinueHistory(userInfo,puzzleInfo,historyInfo) )
			buttons.add( new ImageHyperlink(new Image("ButtonContinue.png"), tokenFactory.getContinueHistoryToken(historyKey)) );
		if( Capabilities.canViewHistory(userInfo,puzzleInfo,historyInfo) )
			buttons.add( new ImageHyperlink( new Image("ButtonPlay.png"), tokenFactory.getViewHistoryToken(historyKey) ) );

		table.setWidget( row, 0, buttons );
		table.setHTML( row, 1, historyInfo.getAuthorUsername(true) );
	}
	

	@Override
	protected boolean isRefreshing() {
		return historyList.isRefreshing();
	}
	
	@Override
	protected boolean isValid() {
		return historyList.isValid();
	}
	@Override
	protected int getListCount() {
		return historyList.getHistoryCount();
	}
	

	@Override
	protected String getStylePrefix() {
		return STYLE_PREFIX;
	}
}
